#!/usr/bin/env bash

cat /etc/hosts | grep "raw.githubusercontent.com" -q
if [ $? -ne 0 ]; then
  echo "199.232.28.133 raw.githubusercontent.com" >>/etc/hosts
  echo "199.232.68.133 raw.githubusercontent.com" >>/etc/hosts
  echo "185.199.108.133 raw.githubusercontent.com" >>/etc/hosts
  echo "185.199.109.133 raw.githubusercontent.com" >>/etc/hosts
  echo "185.199.110.133 raw.githubusercontent.com" >>/etc/hosts
  echo "185.199.111.133 raw.githubusercontent.com" >>/etc/hosts
fi

PROXY_URL=https://ghproxy.com/

##############################  作  者  昵  称  （必填）  ##############################

author_list="Public LongZhuZhu i-chenzhe adolf adolf1 sss"

##############################  作  者  脚  本  地  址  链  接   （必填）  ##############################

scripts_base_url_1=https://gitee.com/SuperManito/scripts/raw/master/
## 龙王的库
scripts_base_url_2=${PROXY_URL}https://raw.githubusercontent.com/nianyuguai/longzhuzhu/main/qx/
## i-chenzhe的库
scripts_base_url_3=${PROXY_URL}https://raw.githubusercontent.com/monk-coder/dust/dust/i-chenzhe/
## 庙里的经书
scripts_base_url_4=${PROXY_URL}https://raw.githubusercontent.com/monk-coder/dust/dust/normal/
scripts_base_url_5=${PROXY_URL}https://raw.githubusercontent.com/monk-coder/dust/dust/member/
scripts_base_url_6=${PROXY_URL}https://github.com/sd462355991111/JDDJ/blob/main/

##############################  作  者  脚  本  名  称  （必填）  ##############################

my_scripts_list_1="jd_paopao.js jd_inter_shopsign.js jd_shop_followsku.js jd_shop_lottery.js jx_cfdtx.js jd_try.js"
my_scripts_list_2="jd_super_redrain.js jd_half_redrain.js"
my_scripts_list_3="z_mother_jump.js z_shop_captain.js"
my_scripts_list_4="adolf_pk.js"
my_scripts_list_5="adolf_flp.js adolf_oneplus.js"
my_scripts_list_6="jddj_fruit.js jddj_fruit_collectWater.js jddj_bean.js jddj_plantBeans.js jddj_getPoints.js"

##############################  随  机  函  数  ##############################
rand() {
  min=$1
  max=$(($2 - $min + 1))
  num=$(cat /proc/sys/kernel/random/uuid | cksum | awk -F ' ' '{print $1}')
  echo $(($num % $max + $min))
}
cd ${ShellDir}
index=1
for author in $author_list; do
  echo -e "开始下载 $author 的活动脚本："
  echo -e ''
  eval scripts_list=\$my_scripts_list_${index}
  #echo $scripts_list
  eval url_list=\$scripts_base_url_${index}
  #echo $url_list
  for js in $scripts_list; do
    eval url=$url_list$js
    echo $url
    eval name=$js
    echo $name
    wget -q --no-check-certificate $url -O scripts/$name.new

    if [ $? -eq 0 ]; then
      mv -f scripts/$name.new scripts/$name
      echo -e "更新 $name 完成...\n"
      croname=$(echo "$name" | awk -F\. '{print $1}')
      script_date=$(cat scripts/$name | grep "http" | awk '{if($1~/^[0-59]/) print $1,$2,$3,$4,$5}' | sort | uniq | head -n 1)
      if [ -z "${script_date}" ]; then
        cron_min=$(rand 1 59)
        cron_hour=$(rand 7 9)
        [ $(grep -c "$croname" ${ListCron}) -eq 0 ] && sed -i "/hangup/a${cron_min} ${cron_hour} * * * bash jd $croname" ${ListCron}
      else
        [ $(grep -c "$croname" ${ListCron}) -eq 0 ] && sed -i "/hangup/a${script_date} bash jd $croname" ${ListCron}
      fi
    else
      [ -f scripts/$name.new ] && rm -f scripts/$name.new
      echo -e "更新 $name 失败，使用上一次正常的版本...\n"
    fi
  done
  index=$(($index + 1))
done

## 京东试用脚本添加取关定时任务
grep -q "（jd_try.js）" ${ListCron} && sed -i "/（jd_try.js）/d" ${ListCron} &&  sed -ie "/5 10 \* \* \* bash jd jd_unsubscribe/d" ${ListCron} ## 修复试用脚本重复通知定时任务的错误

[ -f ${ScriptsDir}/jd_try.js ] && grep -q "5 10 \* \* \* bash jd jd_unsubscribe" ${ListCron}
if [ $? -ne 0 ]; then
  echo -e '\n# 京东试用脚本添加的取关定时任务\n5 10 * * * bash jd jd_unsubscribe' >>${ListCron}
fi

##############################  删  除  失  效  的  活  动  脚  本  ##############################
